<template>
	<view class="">
		<top-heigth :backTitle="backTitle" style="background-color: #fff;"></top-heigth>
					
			<view class="content-box">
				<view class="mine-font">
					<view class="list">
						<view class="list-l">姓名</view>
						<view class="list-r">
							<uv-input placeholder="请输入本人姓名" border="none" v-model="name"></uv-input>
						</view>
					</view>
					<view class="list">
						<view class="list-l">身份证号</view>
						<view class="list-r">
							<uv-input placeholder="请输入身份证号"  border="none" v-model="card"></uv-input>
						</view>
					</view>
				</view>
				<view class="content-box">
					<view @click="getList()" class="login-but2">
					提交
					</view>
				</view>
				
			</view>
			
	</view>
</template>

<script>
	import TopHeigth from '@/components/TopHeigth.vue'
	const aliyunVerify = uni.requireNativePlugin('AP-FaceDetectModule');

	export default {
		data() {
			return {
				backTitle:"实名认证",
				value: '',
				userInfo: uni.getStorageSync('userInfo'),
				name:'',
				card:'',
				userId: 0,
				version: 0,
			}
		},
		components: { TopHeigth },
		onLoad(e) {
			this.userId = e.id
			this.version = e.version
		},
		methods: {			
			getList(){
				var _this = this;
				if(this.name == ''){
					return uni.showToast({
						title: '请填姓名',
						icon: 'none'
					});
				}
				
				let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
				let _IDre15 =  /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
				// 校验身份证：
				    if( !(_IDRe18.test( this.card) || _IDre15.test( this.card))){
				        return uni.showToast({
				        	title: '身份证格式有误',
				        	icon: 'none'
				        });
				    }
					
					let metaInfo = aliyunVerify.getMetaInfo()
					let p = uni.getSystemInfoSync().platform;
					if (p === "ios") {
						metaInfo = JSON.stringify(metaInfo);
					}
					
					// 进行活体检测
					uni.$u.http.post('/api/shop/v1_0/tAppStaffRCA/initFaceVerify',{
						 id:this.userId,
						 staffCardNo:this.card,
						 staffName:this.name,
						 metaInfo: metaInfo,
						 hasShop: true
					}).then((res) => {
						console.log(res)
						if(res.code != 200){
							return uni.showToast({
								title: res.message,
								icon: 'none'
							});
						}
						aliyunVerify.verify({
						    "certifyId":res.data.certifyId,
						  }, 
						  function(response){
							// 请求验证结果
							uni.$u.http.post('/api/shop/v1_0/tAppStaffRCA/describeFaceVerify',{
								certifyId: res.data.certifyId
							}).then(checked => {
								console.log("checked", checked)
								if(checked.data.subCode == 200){
									// 成功
									uni.$u.http.post('/api/shop/v1_0/tAppStaffRCA/updateTAppStaff',{
										id: _this.userId,
										version: _this.version,
										isAuthentication: 1,
										staffName: _this.name,
										staffCardNo: _this.card
									}).then(res => {
										uni.navigateBack()
									})
								}else if(checked.data.subCode == 201){
									// 姓名身份证不匹配
									return uni.showToast({
										title: "姓名和身份证不一致",
										icon: 'none'
									});
								}else{
									// 其他错误请重试
									return uni.showToast({
										title: "认证失败,请重试",
										icon: 'none'
									});
								}
							})
							
						  }
						);
					})

					// uni.$u.http.post('/api/shop/v1_0/tAppStaffRCA/updateTAppStaff',{
					// 	 id:this.userId,
					// 	 version:this.version,
					// 	 staffCardNo:this.card,
					// 	 staffName:this.name,
					// }).then((res) => {
					// 	uni.navigateBack()
					// })
			}
		}
	}
</script>

<style lang="scss" scoped>
	@mixin flex($direction: row) {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: $direction;
	}
	.uv-nav-slot {
		@include flex;
		align-items: center;
		justify-content: space-between;
		border-width: 0.5px;
		border-radius: 100px;
		border-color: #dadbde;
		padding: 3px 7px;
		opacity: 0.8;
	}
	
	.mine-back{
		background: url("/static/image/back2.png") no-repeat;
		background-size: 100% auto;
		
	}
	
	.mine-font{
		background: #fff;
		border-radius: 16rpx;
		//box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0,0,0,0.05);
		font-size: 28rpx;
		color: #02091A;
		margin-top: 24rpx;
		padding: 0 24rpx;
		.list{
			@include flex;
			align-items: center;
			justify-content:space-between;
			padding:28rpx 0;
			border-bottom: 2rpx solid #eee;
			
			.list-l{
				@include flex;
				align-items: center;
				justify-content:flex-start;
				min-width:130rpx;
				font-size: 30rpx;
				font-weight: bold
			}
			.list-r{
				@include flex;
				align-items: center;
				justify-content:flex-start;
				width: calc(100% - 164rpx);
				font-size: 42rpx;
				.list-r-font{
					width: calc(100% - 32rpx);
					color: #333;
					font-size: 24rpx;
					margin-right: 16rpx;
				}
			}
			
		}
		.list:last-child{
			border-bottom:none;
		}
	}
</style>
